package dao;

import com.mongodb.WriteResult;
import entity.Admin;
import entity.User;
import org.bson.types.ObjectId;
import org.junit.Test;
import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.query.FindOptions;
import org.mongodb.morphia.query.Query;
import org.mongodb.morphia.query.UpdateOperations;
import org.mongodb.morphia.query.UpdateResults;
import util.MorphiaUtil;

import java.util.List;

public class UserDAO1 {
    //添加
    public void insert(User user) {
        Datastore ds = MorphiaUtil.getDB();
        ds.save(user);
    }
    //删除
    public void delete(String id) {
        Datastore ds = MorphiaUtil.getDB();
        Query<User> query = ds.createQuery(User.class);
        Query<User> que = query.filter("_id",new ObjectId(id));
        WriteResult delete = ds.delete(que);
        System.out.println(delete);
    }
    //根据id查询
    public User findById(String id) {
        Datastore ds = MorphiaUtil.getDB();
        Query<User> query = ds.createQuery(User.class);
        Query<User> que = query.filter("_id",new ObjectId(id));
        User user = query.get();
        return user;
    }
    //根据name查询
    public User findByName(String name) {
        Datastore ds = MorphiaUtil.getDB();
        Query<User> query = ds.createQuery(User.class);
        User user =null;
        try{
            query.field("username").equal(name);
            user =  query.get();
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
        return user;
    }
    //查询所有
    public List<User> findAll() {
        Datastore ds = MorphiaUtil.getDB();
        Query<User> query = ds.createQuery(User.class);
        List<User> users = query.asList();
        return users;
    }
    //分页查询
    public List<User> findPage(int skip,int limit) {
            Datastore ds = MorphiaUtil.getDB();
            Query<User> query = ds.createQuery(User.class);
            FindOptions findOptions = new FindOptions();
            findOptions.skip(skip).limit(limit);
            // 设置排序
            List<User> u = query.asList(findOptions);
            return u;
    }
    //统计总数
    public long count(){
        //    通过Morphia框架获得database
        Datastore db = MorphiaUtil.getDB();
        //        创建查询条件对象query
        Query<User> query = db.createQuery(User.class);
        long count = query.count();
        return count;
    }
    //修改
    public void update(User user) {
        Datastore ds = MorphiaUtil.getDB();
        Query<User> query = ds.createQuery(User.class);
        query.filter("_id",new ObjectId(user.getId()));
        UpdateOperations<User> update = ds.createUpdateOperations(User.class);
        if (user.getUsername()!=null){
            update.set("username", user.getUsername());
        }
        if (user.getPassword()!=null){
            update.set("password", user.getPassword());
        }
        if (user.getMobile()!=null){
            update.set("mobile", user.getMobile());
        }
        if (user.getEmail()!=null){
            update.set("email", user.getEmail());
        }
        if (user.getAddress()!=null){
            update.set("address", user.getAddress());
        }
        if (user.getStatus()==0 || user.getStatus()==1){
            update.set("status", user.getStatus());
        }
        if (user.getAge()!=null){
            update.set("age", user.getAge());
        }
        UpdateResults u = ds.update(query,update);
        //执行修改;
        System.out.println(u);
    }

    public void updatePerson(User user) {
        Datastore ds = MorphiaUtil.getDB();
        Query<User> query = ds.createQuery(User.class);
        query.filter("username",user.getUsername());
        UpdateOperations<User> update = ds.createUpdateOperations(User.class);
        if (user.getUsername()!=null){
            update.set("username", user.getUsername());
        }
        if (user.getPassword()!=null){
            update.set("password", user.getPassword());
        }
        if (user.getMobile()!=null){
            update.set("mobile", user.getMobile());
        }
        if (user.getEmail()!=null){
            update.set("email", user.getEmail());
        }
        if (user.getAddress()!=null){
            update.set("address", user.getAddress());
        }
        if (user.getStatus()==0 || user.getStatus()==1){
            update.set("status", user.getStatus());
        }
        if (user.getAge()!=null){
            update.set("age", user.getAge());
        }
        if (user.getImgUrl()!=null){
            update.set("imgUrl", user.getImgUrl());
        }
        UpdateResults u = ds.update(query,update);
        //执行修改;
        System.out.println(u);
    }

    // 状态修改（停用启用）
    public void updateStatus(String id){

        Datastore ds = MorphiaUtil.getDB();
        UserDAO1 userDAO1=new UserDAO1();

        User user=userDAO1.findById(id);
        System.out.println(user);

        Query<User> query = ds.createQuery(User.class);
        query.filter("_id",new ObjectId(id));
        UpdateOperations<User> update = ds.createUpdateOperations(User.class);

        if(user.getStatus()==0){
            update.set("status", 1);
            System.out.println(user);

        }
        else if(user.getStatus()==1){

            update.set("status", 0);
            System.out.println(user);

        }

        UpdateResults u = ds.update(query,update);
        //执行修改;
        System.out.println(u);

    }

//    @Test
//    public void TestupdateStatus(){
//
//        UserDAO1 userDAO1=new UserDAO1();
//        userDAO1.updateStatus("668fa8a1ef240000da004e42");
//
//    }



}
